using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace clockerEditor
{
    public enum state
    {
        PLAY,
        FREEZE    

    }


    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        public static SpriteBatch spriteBatch;
        public static Vector2 windowSize;
        public static Vector2 drawOffset = Vector2.Zero;
        UtilityBelt utilityBelt;
        
        public static state state = state.PLAY;
        public static bool quit = false;

        public static string mapName;
        public static string fileName;
        public static string loadFileName;
        public static string tileSheetFileName;

        //initialize the drawable layer
        public static int drawableLayer = 0;

        //im a map ... map
        public static mapClasses.Map map;

        //declare the map and its dimentions for mapping and stuff
        public static int mapHeight = 15;
        public static int mapWidth = 20;
        public static int tileHeight = 32;
        public static int tileWidth = 32;

        //selected tile decleration 
        public static int selectedTileNo = 0;

        //tex me
        public static Texture2D tileSheet;
        public static Texture2D solid;
        public Texture2D pixel;


        MouseState curState;
        KeyboardState prevState;
        SpriteFont basic;



        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            //this.graphics.IsFullScreen = true;
            Content.RootDirectory = "Content";

            map = new mapClasses.Map(mapWidth, mapHeight, tileWidth, tileHeight);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // size of window

            graphics.PreferredBackBufferHeight = 900;
            graphics.PreferredBackBufferWidth = 1500; // yeah i made it a square

            graphics.ApplyChanges();

            //initialize the window size
            windowSize = new Vector2(Window.ClientBounds.Width, Window.ClientBounds.Height);




            //stop the mouse from dissapearing.. aka make it visible
            IsMouseVisible = true;




            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //load tex me
            pixel = Content.Load<Texture2D>(@"PlaceSpritesHere/pixel");
            solid = Content.Load<Texture2D>(@"PlaceSpritesHere/solid");


            //font
            basic = Content.Load<SpriteFont>(@"basic");

            //initialize the utility belt nanananna batman
            utilityBelt = new UtilityBelt(Content);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here


        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (quit)
                this.Exit();

            //map title
            this.Window.Title = "kalada is the gre... i mean mapEditor -" + mapName;

            //Update the selected tile
            KeyboardState keyState = Keyboard.GetState();
            if (selectedTileNo < map.tileSet.Count - 1)
            {
                if (keyState.IsKeyDown(Keys.Up) && !prevState.IsKeyDown(Keys.Up))
                    selectedTileNo++;
            }
            if (selectedTileNo > 0)
            {
                if (keyState.IsKeyDown(Keys.Down) && !prevState.IsKeyDown(Keys.Down))
                    selectedTileNo--;
            }

            //Get input from the mouse
            if (state == state.PLAY && tileSheet != null)
            {
                if (drawableLayer == 0)
                    map.TileLayer1.SetTiles(selectedTileNo + 1);
                else if (drawableLayer == 1)
                    map.TileLayer2.SetTiles(selectedTileNo + 1);
                else if (drawableLayer == 2)
                    map.SolidLayer.SetTiles(1);
            }

            //Update the mouse state
            curState = Mouse.GetState();

            //Update the previous keyboard state
            prevState = keyState;

            //Update scroll values
            map.UpdateUserInput();

            // updating the utilitybelt
            utilityBelt.update();


            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // starting the spriteBatch

            spriteBatch.Begin();

             //Draw map bounds
            spriteBatch.Draw(pixel, new Rectangle(
                -(int)drawOffset.X * map.tileWidth, -(int)drawOffset.Y * map.tileHeight,
                map.mapWidth * map.tileWidth,
                map.mapHeight * map.tileHeight), Color.White);

            // drawing the belt and map
            utilityBelt.draw();
            map.DrawMap();

             //Draw the selected tile overlay
            if (tileSheet != null && drawableLayer != 2)
                spriteBatch.Draw(tileSheet, new Vector2(curState.X - tileWidth / 2, curState.Y - tileHeight / 2),
                    map.tileSet[selectedTileNo], Color.White);

            //Show the user which layer they are drawing on
            string layerText = "";

            if (drawableLayer == 0)
                layerText = "Layer 1";
            else if (drawableLayer == 1)
                layerText = "Layer 2";
            else if (drawableLayer == 2)
                layerText = "Collision Layer";

            base.Draw(gameTime);

            //ending the spriteBatch here so any object can access the spriteBatch and draw itself

            spriteBatch.End();
        }
    }
}
